﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using NewsMine.DomainObjects;

namespace NewsDiscoveryEngine.Classification
{
    public class UrlBasedTagsDetectionClassifier : IClassifier
    {
        //todo: needs to be implemeted.

        //* Need to take the url of the menu and take the segement words as tags; Probably we can remove the extention of the page like .aspx .php and remaining stuff becomes the tag info for us. *
        public NewsMine.DomainObjects.FeedItem Classify(NewsMine.DomainObjects.FeedItem feedItem)
        {

            List<string> tagResults = new List<string>();
            tagResults.AddRange(GetTagsFromMenuItem(feedItem.RefUrl));
            //tagResults.AddRange(GetTagsFromUrl(feedItem.Link));

            bool isTagExists = false;

            if (feedItem.Tags == null || feedItem.Tags.Count == 0)
            {
                feedItem.Tags = tagResults;
            }
            else
            {
                foreach (string tag in tagResults)
                {
                    isTagExists = false;
                    foreach (string existingTag in feedItem.Tags)
                    {
                        if (tag.ToLower() == existingTag.ToLower())
                        {
                            isTagExists = true;
                            break;
                        }
                    }

                    if (isTagExists == false)
                    {
                        feedItem.Tags.Add(tag);
                    }
                }
            }

            return feedItem;
        }


        public static List<string> GetTagsFromUrl(string url)
        {
            Uri tempUri = new Uri(url);

            var segments = tempUri.Segments;

            List<string> resultedTags = new List<string>();


            string tempString = null;
            for (int i = 0; i < segments.Length; i++)
            {

                tempString = segments[i].ToLower();

                //UrlDecode - System.Web.HttpUtility.UrlDecode("%e0%b2%b5%e0%b2%bf%e0%b2%a6%e0%b3%87%e0%b2%b6") => "ವಿದೇಶ"

                tempString = HttpUtility.UrlDecode(tempString);
                tempString = tempString.TrimEnd('/').TrimStart('/');

                if (string.IsNullOrEmpty(tempString))
                    continue;
                // if this is an file name of the url like ( health.aspx / sports.html), we just need to remove the extension.
                if (i == segments.Length - 1)
                {
                    if (tempString.Contains("."))
                    {
                        tempString = tempString.Split('.')[0]; //todo: if we have more than one 
                    }
                }

                resultedTags.Add(tempString);
            }

            return resultedTags;
        }

        static List<WebSiteMenu> menuItemsFromRepository = null;

        static DateTime lastMenuItemsRefreshedTime = DateTime.MinValue;

        public static List<string> GetTagsFromMenuItem(string menuRefUrl)
        {
            RefreshMenuItems();

            List<string> resultedTags = new List<string>();

            var resultedMenuItem = (from mnuItem in menuItemsFromRepository
                                    where mnuItem.Url.ToLower() == menuRefUrl.ToLower()
                                    select mnuItem).FirstOrDefault<WebSiteMenu>();

            if (resultedMenuItem == null)
                return new List<string>();

            if (!string.IsNullOrEmpty(resultedMenuItem.Title))
            {
                resultedTags.Add(resultedMenuItem.Title);
            }

            //if (resultedMenuItem.Tags != null && resultedMenuItem.Tags.Count > 0)
            //{
            //    resultedTags.AddRange(resultedMenuItem.Tags);
            //}

            return resultedTags;

        }

        private static void RefreshMenuItems()
        {
            if (menuItemsFromRepository == null || lastMenuItemsRefreshedTime.AddHours(1) < DateTime.UtcNow)
            {
                lastMenuItemsRefreshedTime = DateTime.UtcNow;
                menuItemsFromRepository = NewsMine.DataManagers.WebSiteInfoManager.GetAllMenuItems();
            }
        }

    }
}
